
jQuery.support.cors = true;


function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}

function GetXSRF(rnd) {
    var rnd = rnd + rnd;
    return $("meta[name='csrf_token']").attr('content');
}

function GetXSRFCOOKE(name) {
    var c = document.cookie.match("\\b" + name + "=([^;]*)\\b");
    return c ? c[1] : undefined;
}


function rnd(n) {
    if (n == "" || n == null) n = 100
    return Math.random().toString().replace("0.", "");
}


function pagelocation(str) {
    window.location = str;
}


function GetTimeStamp(dateTimer) {
    var _nowTimer = new Date();
    if (dateTimer != "" && dateTimer != null) _nowTimer = new Date(_nowTimer);
    var _timestamp = Date.parse(_nowTimer);
    _timestamp = _timestamp / 1000;
    return _timestamp;
}


function checkRates(str) {
    var re = /^(([1-9][0-9]*\.[0-9][0-9]*)|([0]\.[0-9][0-9]*)|([1-9][0-9]*)|([0]{1}))$/
    return re.test(str);
}

function keydownBorderColor(obj) {
    //obj.css("background-color", "#ccc");
    obj.attr("style", "border:1px solid #f39c12")
    setTimeout(function () {
        obj.attr("style", "border:0px")
    }, 100);
}

function plusXing(str) {
    return str.substr(0, 3) + "****" + str.substr(7);
}

function getCheckboxval(obj) {
    return (obj.is(':checked') ? "1" : "0");
}


$.postJSON = function (url, data, header, callback) {
    //$(".pageload").show("正在请求数据服务器...");
    header = (null == header ? {} : header);
    data.csrf_token = $("meta[name='csrf_token']").attr('content'); //GetXSRF(rnd());//$("meta[name='csrf_token']").attr('content'); //GetXSRFCOOKE('GetXSRF');
    jQuery.ajax({
        url: url,
        data: jQuery.param(data),
        dataType: "json",
        type: "POST",
        headers: header,
        success: callback
    }).always(function () {
        //pageloadhide();
    });
}



$.postCORS = function (url, data, header, callback) {
    //$(".pageload").show("正在请求数据服务器...");
    header = (null == header ? {} : header);
    data.csrf_token = GetXSRF(); //GetXSRFCOOKE('GetXSRF');
    jQuery.ajax({
        url: url,
        data: jQuery.param(data),
        dataType: "json",
        type: "POST",
        headers: header,
        success: callback
    }).always(function () {
    });
}



/*---------------------push--------------------*/
jQuery.requestInput=function(DatasInput) {
    ///////pageloadshow("正在获取数据...");
    var _data = {};
    $(DatasInput).each(function () {
        var objInfo = $(this);
        if (objInfo.attr("type") == "text" ||
            objInfo.attr("type") == "password" ||
            objInfo.attr("type") == "hidden" ||
            objInfo.prop("tagName") == "TEXTAREA" ||
            objInfo.prop("tagName") == "SELECT") {
            _data[objInfo.attr("id")] = objInfo.val();
        }
    });
    return _data;
}


function reluesJsonfun(relues, returnUrl) {
    //alert(returnUrl)
    //pageloadhide();
    if (parseInt(relues.status) < 1) {
        if(relues.msg.length>0 && relues.msg!='None'){
            ShowErr(relues.relues,relues.msg)
            return false
        }
        get_error_message(relues.relues)
        return false;
    }

    if(relues.msg.length>0 && relues.msg!='None'){
        alert(relues.msg);
        return false;
    }

    if (returnUrl != "" && returnUrl != null) {
        if (parseInt(relues.status)>0) {
            returnUrl = returnUrl.replace('{$=status}', relues.status);
            pagelocation(returnUrl);
        }
    }
    return false;

}

function get_error_message(relues) {
    obj = Object.keys(relues)
   // alert(relues)
    if(obj.length >0){
        _input_name = obj[0]
        _input_value = relues[_input_name]
       ShowErr(_input_name,_input_value)
        return false
    }
    alert('服务器异常！请联系管理员！')
    return false
}


function ShowErr(obj, msg) {
    alert(msg);
    $("#" + obj).focus();
    return false;
}





/*---------------------Date--------------------*/
Date.prototype.format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1,                 //月份
        "d+": this.getDate(),                    //日
        "h+": this.getHours(),                   //小时
        "m+": this.getMinutes(),                 //分
        "s+": this.getSeconds(),                 //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
}
/*---------------------string.format--------------------*/
String.prototype.format = function (args) {
    var result = this;
    if (arguments.length > 0) {
        if (arguments.length == 1 && typeof (args) == "object") {
            for (var key in args) {
                if (args[key] != undefined) {
                    var reg = new RegExp("({" + key + "})", "g");
                    result = result.replace(reg, args[key]);
                }
            }
        }
        else {
            for (var i = 0; i < arguments.length; i++) {
                if (arguments[i] != undefined) {
                    var reg = new RegExp("({)" + i + "(})", "g");
                    result = result.replace(reg, arguments[i]);
                }
            }
        }
    }
    return result;
}

/*---------------------string.toFixed--------------------*/
Number.prototype.toFixed = function (len) {
    var add = 0;
    var s, temp;
    var s1 = this + "";
    var start = s1.indexOf(".");
    if (s1.substr(start + len + 1, 1) >= 5) add = 1;
    var temp = Math.pow(10, len);
    s = Math.floor(this * temp) + add;
    return s / temp;
}




function Isdate(_date_) {
    if (null == _date_) return false;
    if (_date_.length < 5) return false;
    var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
    var result = _date_.match(reg);
    if (result == null)
        return false;
    return true;
    //var d = new Date(result[1], result[3] - 1, result[4]);
    //return (d.getFullYear() == result[1] && (d.getMonth() + 1) == result[3] && d.getDate() == result[4]);
}
